﻿using Newtonsoft.Json;
using OASystem.Attributes;
using OASystem.BLL;
using OASystem.BLL.IService;
using OASystem.BLL.Service;
using OASystem.Common;
using OASystem.Model;
using OASystem.Models;
using OASystem.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace OASystem.Controllers
{

    public class HomeController : Controller
    {

        public string AccessToken = "";

        #region 注入
        private ILoginService _loginService;
        private IRecode_tail01Service _recode_tail01Service;
        private IAnnouncementService _AnnouncementService;
        private IDossier_myService _dossier_MyService;
        public HomeController(ILoginService loginService, IRecode_tail01Service recode_tail01Service, IAnnouncementService AnnouncementService, IDossier_myService dossier_MyService)
        {
            _loginService = loginService;
            _recode_tail01Service = recode_tail01Service;
            _AnnouncementService = AnnouncementService;
            _dossier_MyService = dossier_MyService;
        }
        #endregion

        //[PermissionAttribute]
        #region Index
        public ActionResult Index()
        {
            Expression<Func<Userinfo, bool>> lambdaExpression = a => true;
            ViewBag.aa = _loginService.QueryList(lambdaExpression);
            return View();
        }
        #endregion
        #region Person_Update
        public ActionResult Person_Update()
        {
            ViewBag.User = PersonController.user;
            return View();
        }
        #endregion
        #region Dossier_myList
        public ActionResult Dossier_myList()
        {

            return View();
        }
        #endregion
        #region Access_list
        public ActionResult Access_list()
        {
            return View();
        }
        #endregion
        #region Access_tail
        public ActionResult Access_tail()
        {
            return View();
        }
        #endregion
        #region Book01
        public ActionResult Book01()
        {
            return View();
        }
        #endregion
        #region Calendar
        public ActionResult Calendar()
        {
            return View();
        }
        #endregion
        #region Data
        public ActionResult Data()
        {
            return View();
        }
        #endregion
        #region Dossier_my
        public ActionResult Dossier_my()
        {
            //查询所有的档案
            Expression<Func<Httppostedfile, bool>> lambdaExpression = a => a.UserId == AdminContext.context.adminUser.UserId;
            ViewBag.Dossier = _dossier_MyService.QueryList(lambdaExpression);
            return View();
        }
        #endregion
        public ActionResult Dossier_publicList()
        {

            return View();
        }
        public ActionResult TakeAbout()
        {

            return View();
        }

        #region Dossier_puplic
        public ActionResult Dossier_puplic()
        {
            Expression<Func<Httppostedfile, bool>> lambdaExpression = a => a.ispublic == 1;
            ViewBag.Dossierpunlic = _dossier_MyService.QueryList(lambdaExpression);
            return View();
        }
        #endregion
        #region Email
        public ActionResult Email()
        {
            return View();
        }
        #endregion
        #region Fcontract_sign
        public ActionResult Fcontract_sign()
        {
            return View();
        }
        #endregion
        #region Fcontrat_tail
        public ActionResult Fcontrat_tail()
        {
            return View();
        }
        #endregion
        #region File_sign
        public ActionResult File_sign()
        {
            return View();
        }
        #endregion
        #region Filesign_tail
        public ActionResult Filesign_tail()
        {
            return View();
        }
        #endregion
        #region Ahome
        public ActionResult Ahome()
        {
            Userinfo Userinfo = AdminContext.context.adminUser;
            //查询id相关的对象
            Expression<Func<Userinfo, bool>> lambdaExpression = a => a.UserId == Userinfo.UserId;
            Userinfo userinfos = _loginService.Query(lambdaExpression);
            #region 查看所有通知并在AHome中动态显示
            //Lambda表达式查看通知所有的标题信息
            Expression<Func<Announcement, bool>> WhereLambda = a => a.Publisher == userinfos.UserName;
            //接收集合部门表
            List<Announcement> departinfos = _AnnouncementService.QueryList(WhereLambda);
            ViewBag.departionfos = departinfos;
            #endregion
            return View();
        }
        #endregion
        #region Hotel_data
        public ActionResult Hotel_data()
        {
            return View();
        }
        #endregion
        #region Hotel_tail
        public ActionResult Hotel_tail()
        {
            return View();
        }
        #endregion
        #region HotelStatic
        public ActionResult HotelStatic()
        {
            return View();
        }
        #endregion
        #region LocationStatic
        public ActionResult LocationStatic()
        {
            return View();
        }
        #endregion
        #region SecondPage
        public ActionResult SecondPage()
        {
            return View();
        } 
        #endregion


        #region Login
        public ActionResult Login()
        {

            #region 钉钉
            ////dingf4119029a8b5980024f2f5cc6abecb85
            //string CorpId = "dingf4119029a8b5980024f2f5cc6abecb85";//每一个个人应用都会分配一个AppId，该AppId是个人应用开发过程中的唯一性标识，用来获取登录用户授权的access_token。
            //string CorpSecret = "GjTmMpIKA8H-JToX7_XOAjTqpuJ5TtjP-Gvs5N37-_rO6VzKQqSRNN4vU_GSiTZl";//每一个个人应用都会分配一个AppSecret，AppSecret可用来获取登录用户授权的access_token。  
            //string AccessUrl = string.Format("https://oapi.dingtalk.com/gettoken?corpid={0}&corpsecret={1}", CorpId, CorpSecret);
            //Newtonsoft.Json.Linq.JToken json = Newtonsoft.Json.Linq.JToken.Parse(DingDingApi.HttpGet(AccessUrl));
            //AccessToken = json["access_token"].ToString();

            //string MessageUrl = "https://oapi.dingtalk.com/message/send?access_token=" + AccessToken;
            //var json_req = new
            //{
            //    touser = "manager6876",  //接受推送userid，不同用户用|分割
            //    toparty = "149448916",   //接受推送部门id
            //    agentid = "330892626",
            //    msgtype = "text", //推送类型
            //    text = new
            //    {
            //        content = "你好呀!!!"
            //    }
            //};
            //string jsonRequest = JsonConvert.SerializeObject(json_req);//将对象转换为json
            //DingDingApi.HttpPost(MessageUrl, jsonRequest); 
            #endregion

            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get("CookieName");
            if (cookie != null)
            {
                string name = cookie["UserName"];//等同于string name = cookie.Values.Get("UserName");
                string pwd = cookie["Password"];
                DateTime time = Convert.ToDateTime(cookie["time"]);

                if (name != null && pwd != null && time != null && DateTime.Now < time)
                {
                    //将Cookie中的值赋给上下文session  使其在不登录时页面也能够显示
                    AdminContext.context.adminUser = new Userinfo()
                    {
                        UserName = name,
                        Password = pwd
                    };
                    return Redirect("/Home/Index");
                }
            }
            return View();
        }
        #endregion
        #region Login1
        public ActionResult Login1()
        {
            HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies.Get("CookieName");
            if (cookie != null)
            {
                string name = cookie["UserName"];//等同于string name = cookie.Values.Get("UserName");
                string pwd = cookie["Password"];
                DateTime time = DateTime.Parse(cookie["time"]);

                if (name != null && pwd != null && time != null && DateTime.Now < time)
                {
                    //将Cookie中的值赋给上下文session  使其在不登录时页面也能够显示
                    AdminContext.context.adminUser = new Userinfo()
                    {
                        UserName = name,
                        Password = pwd
                    };
                    return Redirect("/Home/Index");
                }
            }
            return View();
        }
        #endregion
        #region MailList
        public ActionResult MailList()
        {
            PartService partService = new PartService();
            Expression<Func<Departinfo, bool>> lambdaExpression1 = a => a.Grade == 1;
            //二级lambda表达式
            Expression<Func<Departinfo, bool>> lambdaExpression2 = a => a.Grade == 2;
            ViewBag.one = partService.QueryList(lambdaExpression1);
            //调用分页
            ViewBag.two = partService.QueryList(lambdaExpression2);
            return View();
        }
        #endregion
        #region MailList_tail
        public ActionResult MailList_tail()
        {
            return View();
        }
        #endregion
        #region Mesage
        public ActionResult Mesage()
        {
            return View();
        }
        #endregion
        #region Mesage_tail
        public ActionResult Mesage_tail()
        {
            return View();
        }
        #endregion
        #region Myfile_data
        public ActionResult Myfile_data()
        {
            return View();
        }
        #endregion
        #region Myrecode
        public ActionResult Myrecode()
        {
            return View();
        }
        #endregion
        #region Mytrain
        public ActionResult Mytrain()
        {

            return View();
        }
        #endregion
        #region Mywork
        public ActionResult Mywork()
        {
            return View();
        }
        #endregion
        #region Myleave
        public ActionResult Myleave()
        {
            return View();
        }
        #endregion
        #region Notic_tail
        public ActionResult Notic_tail()
        {
            return View();
        }
        public ActionResult Notic_Update()
        {
            ViewBag.User = AnnouncementController.user;

            return View();
        }
        #endregion
        #region Notice
        public ActionResult Notice()
        {
            return View();
        }
        #endregion
        #region Part
        public ActionResult Part()
        {
            PartService partService = new PartService();
            //一级lambda表达式
            Expression<Func<Departinfo, bool>> lambdaExpression1 = a => a.Grade == 1;
            //二级lambda表达式
            Expression<Func<Departinfo, bool>> lambdaExpression2 = a => a.Grade == 2;
            ViewBag.one = partService.QueryList(lambdaExpression1);


            //调用分页
            ViewBag.two = partService.QueryList(lambdaExpression2);
            return View();
        }
        #endregion
        #region Part_tail
        public ActionResult Part_tail()
        {
            return View();
        }
        public ActionResult Part_update()
        {
            ViewBag.User = PartController.user;
            return View();
        }
        public ActionResult Part_delect()
        {
            ViewBag.User = PartController.user;
            return View();
        }
        #endregion
        #region Person
        public ActionResult Person()
        {
            return View();
        }
        #endregion
        #region Person_tail
        public ActionResult Person_tail()
        {
            return View();
        }
        #endregion
        #region Ph_list
        public ActionResult Ph_list()
        {
            return View();
        }
        #endregion
        #region Recode
        public ActionResult Recode()
        {
            return View();
        }
        #endregion
        #region Recode_tail
        public ActionResult Recode_tail()
        {
            return View();
        }
        #endregion
        #region 制定任务:添加
        public ActionResult Recode_tail01()
        {
            ResponseResult result = new ResponseResult();
            Userinfo userinfo = AdminContext.context.adminUser;
            result = _recode_tail01Service.Select2();
            ViewBag.List = result.data;
            return View();
        }
        #endregion
        #region Role
        public ActionResult Role()
        {
            return View();
        }
        #endregion
        #region Role_Tail
        public ActionResult Role_Tail()
        {

            return View();
        }
        public ActionResult Role_Update()
        {
            ViewBag.User = RoleController.position;
            return View();
        }
        #endregion
        #region Rules
        public ActionResult Rules()
        {
            return View();
        }
        #endregion
        #region Schedule
        public ActionResult Schedule()
        {
            return View();
        }
        #endregion
        #region Summary_meeting
        public ActionResult Summary_meeting()
        {
            return View();
        }
        #endregion
        #region Summary_special
        public ActionResult Summary_special()
        {
            return View();
        }
        #endregion
        #region Train
        public ActionResult Train()
        {
            return View();
        }
        #endregion
        #region Train_tail
        public ActionResult Train_tail(int id)
        {

            //UserIndex页面传一个id判断改添加操作还是修改操作
            if (id != 0)
            {
                SelectListService modueService = new SelectListService();
                //读取信息调用SelectUserInfo方法
                ViewBag.info = modueService.SelectInfoMangers(id);
            }


            return View();

        }
        #endregion
        #region Train_tail01
        public ActionResult Train_tail01()
        {
            return View();
        }
        #endregion
        #region Work
        public ActionResult Work()
        {
            return View();
        }
        #endregion
        #region Work_tail
        public ActionResult Work_tail()
        {
            return View();
        }
        #endregion
        #region Write_email
        public ActionResult Write_email()
        {
            return View();
        }
        #endregion


    }
}